package com.netease.research.controller.seller;

import com.netease.research.Vo.ProductListVo;
import com.netease.research.common.Const;
import com.netease.research.common.ServerResponse;
import com.netease.research.common.StatusCode;
import com.netease.research.pojo.Product;
import com.netease.research.pojo.User;
import com.netease.research.service.IFileService;
import com.netease.research.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.ls.LSResourceResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;

    @RequestMapping("list_all_product.do")
    @ResponseBody
    public ServerResponse<List<ProductListVo>> listAll(){
        return iProductService.listAll();
    }

    @RequestMapping("list_unbuy.do")
    @ResponseBody
    public ServerResponse<List<ProductListVo>> listUnBuy(HttpSession session){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null) {
            return iProductService.listUnBuy();
        }
        return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
    }

//    需要返回ID
    @RequestMapping("publish.do")
    @ResponseBody
    public ServerResponse<Product> publishProduct(HttpSession session,Product product){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if(user==null) {
            return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
        }
        return iProductService.publishProduct(product);
    }

    @RequestMapping("show.do")
    @ResponseBody
    public ServerResponse<Product> show(Integer productId){
        return iProductService.show(productId);
    }

    @RequestMapping("edit.do")
    @ResponseBody
    public ServerResponse<Product> edit(HttpSession session,Product product){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if(user==null) {
            return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
        }
        return iProductService.edit(product);
    }

    @RequestMapping("upload_pic.do")
    @ResponseBody
    public ServerResponse<Map<String,String>> upload(HttpSession session ,
                                                     @RequestParam(value="upload_file", required = false) MultipartFile file,
                                                     HttpServletRequest request){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
        }
        String path = "/var/image";
        String targetFileName = iFileService.upload(file, path);
        String url = new StringBuilder().append("image.wangyushuang.top/").append(targetFileName).toString();
        Map<String,String> fileMap = new HashMap<>();
        fileMap.put("uri",targetFileName);
        fileMap.put("url",url);
        return ServerResponse.createBySuccess(fileMap);
    }

    @RequestMapping("delete_product.do")
    @ResponseBody
    public ServerResponse delete(HttpSession session ,Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
        }
//        TODO 无权限
        return iProductService.delete(productId);
    }
}
